package net.midnighttd.game.view;

import net.midnighttd.engine.view.EngineView;
import net.midnighttd.engine.entity.Entity;
import net.midnighttd.engine.entity.Pathfinder.Pathfinder;
import net.midnighttd.engine.entity.EntityList;
import net.midnighttd.game.entity.Tower;
import java.awt.*;


/**
 * Author: Adam Scarr
 * Date: 22/04/2009
 * Time: 8:09:32 PM
 */
public class MidnightTDView extends EngineView {
    private MainUI ui;

    public MidnightTDView(MainUI ui) {
        super(ui.getGame());
        this.ui = ui;
    }

    public void drawMouse(Graphics g, Point mouse) {
        if(engine.getMapManager() == null) return;

        Point mouseMap = engine.getMapManager().getWorldLocation(mouse);
        Dimension tile = engine.getMapManager().getTileSize();

        int tileX = mouseMap.x * tile.width;
        int tileY = mouseMap.y * tile.height;

        boolean entUnderMouse = engine.getEntityManager().getActiveEntsAt(mouseMap.x, mouseMap.y).size() != 0;
        if(entUnderMouse) {
            // draw a selection mouse, let the user know they can click on existing entities.
            g.setColor(new Color(38,35,33));
            g.drawLine(tileX, tileY, tileX + 5, tileY);
            g.drawLine(tileX, tileY, tileX, tileY + 5);

            g.drawLine(tileX + tile.width, tileY, tileX + tile.width - 5, tileY );
            g.drawLine(tileX + tile.width, tileY , tileX + tile.width, tileY + 5);

            g.drawLine(tileX + tile.width, tileY + tile.height, tileX + tile.width - 5, tileY + tile.height);
            g.drawLine(tileX + tile.width, tileY + tile.height, tileX + tile.width, tileY + tile.height - 5);

            g.drawLine(tileX, tileY + tile.height, tileX + 5, tileY + tile.height);
            g.drawLine(tileX, tileY + tile.height, tileX, tileY + tile.height - 5);

        } else if( ui.getTowerSelector().getActiveTower() != null) {
            int rangeX = (int)(ui.getTowerSelector().getActiveTower().getRange() * tile.width * 2);
            int rangeY = (int)(ui.getTowerSelector().getActiveTower().getRange() * tile.height * 2);

            g.setColor(new Color(0.4f, 0.6f, 0.39f, 0.5f));
            g.drawOval(tileX + (tile.width - rangeX) / 2, tileY + (tile.height - rangeY) / 2, rangeX, rangeY);
            g.drawRect(tileX, tileY, tile.width, tile.height);

            g.setColor(new Color(0.4f, 0.6f, 0.39f, 0.2f));
            g.fillOval(tileX + (tile.width - rangeX) / 2, tileY + (tile.height - rangeY) / 2, rangeX, rangeY);
            g.fillRect(tileX, tileY, tile.width, tile.height);
        }
    }

    public void draw(Graphics g) {
        if(ui.getGame().isOver()) {
            int width = 100;
            int height = 50;
            int x = getWidth() / 2 - width / 2;
            int y = getHeight() / 2 - height / 2;
            g.setColor(new Color(85, 92, 56));
            g.fillRect(x, y, width, height);

            g.setColor(new Color(34,30,38));
            g.drawRect(x, y, width, height);
            g.setColor(Color.WHITE);
            g.drawString("Game Over", x + 20, y + 30);
        }
    }

    public void mouseClicked(Point tile) {
        if(ui.getGame().isOver()) {
            ui.getGame().reset();
        } else {
            Tower t = ui.getTowerSelector().getActiveTower();
            EntityList entsUnderMouse = ui.getGame().getEntityManager().getActiveEntsAt(tile.x, tile.y);


            if(t != null && ui.getGame().getMoney() >= t.getCost() && entsUnderMouse.size() == 0) {
                Entity ent = ui.getGame().getEntityManager().createEntityAt(tile.x, tile.y, t.getName());
                Pathfinder pf = ui.getGame().getEntityManager().getPathfinder();
                Point start = ui.getGame().getPoint("spawn");
                Point end = ui.getGame().getPoint("despawn");

                if(pf.searchNow(start.x, start.y, end.x, end.y, false) == null) {
                    ui.getGame().getEntityManager().removeActiveEnt(ent);
                }
            } else {
                t = (Tower)entsUnderMouse.getFirst("tower");

                if(t != null) {
                    ui.getTowerSelector().displayEntity(t);
                }
            }
        }
    }
}
